package com.pzc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.luojun.entity.User;
import com.luojun.util.HibernateUtil;
import com.pzc.db.DBUtil;


public class UserDao {

	protected Connection conn;
	protected PreparedStatement preparedStatement;
	protected ResultSet resultSet;
	
	//判断帐密是否正确
//	public boolean login(User u) {
//		boolean flag = false;
//		try {
//			int count = 0;
//			conn=DBUtil.getConnection();
//			String sql = "select count(*) as count from users where User_name = ? and password = ?";
//			preparedStatement = conn.prepareStatement(sql);
//			preparedStatement.setString(1, u.getUserName());
//			preparedStatement.setString(2, u.getPassword());
//			resultSet = preparedStatement.executeQuery();//返回结果记录
//			while(resultSet.next()) {
//				count = resultSet.getInt("count");
//			}
//			if(count != 0) {
//				flag = true;
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return false;
//	}
	//判断帐密是否正确
	public boolean loginistrue(String username,String password) {
		Session session = null;
		Transaction transaction = null;
		User user = null;
			session = HibernateUtil.openSession();
			transaction = session.beginTransaction();
			 Query q = session.createQuery(" from User u  where u.userName = ? and u.password = ? ");
			 q.setString(0, username);
			 q.setString(1, password);
			 
			 transaction.commit();// 5.提交事务
			 
		
			 if(q.list().isEmpty()){ //只查看获得的数据list里的第一条数据（下表为0）
				return false;
			 }else{
				 return true;
			 }
	}
	
	
	
	public User login(String username,String password) {
		Session session = null;
		Transaction transaction = null;
		User user = null;
		try {
			session = HibernateUtil.openSession();
			transaction = session.beginTransaction();
			 Query q = session.createQuery(" from User u  where u.userName = ? and u.password = ? ");
			 q.setString(0, username);
			 q.setString(1, password);
			 
			 if(q.list().get(0) != null){ //只查看获得的数据list里的第一条数据（下表为0）
				 System.out.println(111111);
				return (User)q.list().get(0);
			 }
			 transaction.commit();// 5.提交事务
		    } catch (Exception e) {
		      e.printStackTrace();
		      transaction.rollback();
		    } finally {
		      if (null != session) {
		    	//6.关闭session
		        session.close();
		      }
		     
		    }
		    return user;
	}
	
	//添加用户
	public  int addUser(User u) {
		int count = 0;
	    Session session = null;// 1. 打开一个 Session
	    Transaction tran = null;// 2. 开启一个事务
	    try {
	      session = HibernateUtil.openSession();
	      tran = session.beginTransaction();
//---------------------------------
	      session.save(u);//4.保存对象
	      
//	      Query q = session.createQuery(" from User u  where  u.userId=?  ");
//	      q.setString(0, "");
//	      User use =(User) q.list().get(0);
//	      List<User> user = q.list();
	      
//---------------------------------
	      tran.commit();// 5.提交事务
	      count=1;
	    } catch (Exception e) {
	      e.printStackTrace();
	      tran.rollback();
	    } finally {
	      if (null != session) {
	    	//6.关闭session
	        session.close();
	      }
	      return count;
	    }
	  }
	//删除用户
	public User DeleteById(int id) {
		Session session = null;// 1. 打开一个 Session
	    Transaction tran = null;// 2. 开启一个事务
	    try {
	      session = HibernateUtil.openSession();
	      tran = session.beginTransaction();
//---------------------------------
	     User u = session.get(User.class, id);
	      System.out.println(u);
	      session.delete(u);//4.保存对象
	      
//	      Query q = session.createQuery(" from User u  where  u.userId=?  ");
//	      q.setString(0, "");
//	      User use =(User) q.list().get(0);
//	      List<User> user = q.list();
//---------------------------------
	      tran.commit();// 5.提交事务   
	    } catch (Exception e) {
	      e.printStackTrace();
	      tran.rollback();
	    } finally {
	      if (null != session) {
	    	//6.关闭session
	        session.close();
	      }
	    }
	    return null;
	  }
	
//	public int addUser(User u) {
//		int count = 0;
//		try {
//			conn=DBUtil.getConnection();
//			String sql = "insert into user(username,password) values(?,?)";
//			preparedStatement = conn.prepareStatement(sql);
//			preparedStatement.setString(1, u.getUserName());
//			preparedStatement.setString(2, u.getPassword());
//			count = preparedStatement.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return count;
//	}
	
	//查询用户是否存在
	public User isExist(String userName) {
int count = 0;
		
	    Session session = null;// 1. 打开一个 Session
	    Transaction tran = null;// 2. 开启一个事务
	    User user = null;
	    try {
	      session = HibernateUtil.openSession();
	      tran = session.beginTransaction();
//---------------------------------
//	      session.save(u);//4.保存对象
	      
	      Query q = session.createQuery(" from User u  where u.userName = ? ");
	      q.setString(0, userName);
	      if(q.list().get(0)==null)  {
	    	  return user;
	      }
	      
	      user =(User) q.list().get(0);
//	      List<User> list = new ArrayList<User>();
//	      list = q.list();
//	      list.get();
	      
//	      q.setString(0, "");
//	      User use =(User) q.list().get(0);
//	      List<User> user = q.list();
//---------------------------------
	      tran.commit();// 5.提交事务
	      count=1;
	    } catch (Exception e) {
	      e.printStackTrace();
	      tran.rollback();
	    } finally {
	      if (null != session) {
	    	//6.关闭session
	        session.close();
	      }
	     
	    } 
	    return user;
	  }
	
	
	
	
	

	
}
